package com.example.win.android_banduyujia.http.request;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;

import com.example.win.android_banduyujia.http.response.BaseHttpResponse;
import com.example.win.android_banduyujia.manager.ExecutorsManager;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseActivity;

/**
 * Created by Win on 2015/8/6.
 */
public class AsycHttpRequest{

    public static final int CODE_TASK_START = 1;
    public static final int CODE_TASK_END = 2;
    private BaseActivity    mContext;
    private ProgressDialog  mDialog;

    private Handler  mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
           switch (msg.what){
               case CODE_TASK_START:
                    showLoading();
                   break;
               case CODE_TASK_END:
                   hideLoading();
                   break;
           }
            return  true;
        }
    });

    public void doRequest(BaseActivity context, final BaseHttpRequest request,
                     final Class<? extends BaseHttpResponse> clzz, final Handler callbackHandler, final boolean isShowProgressDialog){
       mContext = context;
       Thread thread = new Thread(new Runnable() {
           @Override
           public void run() {
                 request.doRequest(mContext, clzz, callbackHandler, isShowProgressDialog, mHandler);
           }
       });
        thread.setName("http request");
        ExecutorsManager.getInstance().excute(thread);
    }

    private void showLoading() {

    }

    private void hideLoading() {
        if(mDialog !=null){
            mDialog.dismiss();
        }
    }


}
